package com.spa.infrastructure.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void geoAdd(String key, String member, Point point) {
        stringRedisTemplate.opsForGeo().add(key, point, member);
    }

    /**
     * key是否存在
     * @param key 键
     */
    public Boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * 删除
     * @param key 键
     */
    public Boolean delete(String key) {
        return stringRedisTemplate.delete(key);
    }

    /**
     * string类型 通过key获取值
     * @param key 键值
     * @return value
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 获取有效时间
     * @param key
     * @return
     */
    public Long getExpire(String key) {
        return stringRedisTemplate.getExpire(key);
    }

    /**
     * String类型  给key设置值
     * @param key 键
     * @param value 值
     */
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * String类型  给key设置值，并设置过期时间，单位（秒）
     * @param key 键
     * @param value 值
     * @param timeout 超时时间
     */
    public void set(String key, String value, long timeout) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     *  加锁
     **/
    public Boolean getLock(String key,String value){
        Boolean lockStatus = stringRedisTemplate.opsForValue().setIfAbsent(key,value, Duration.ofSeconds(10));
        return lockStatus;
    }

    /**
     *  释放锁
     **/
    public Long releaseLock(String key,String value){
        String luaScript = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
        RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript,Long.class);
        Long releaseStatus = (Long)stringRedisTemplate.execute(redisScript, Collections.singletonList(key),value);
        return releaseStatus;
    }

    public Long add(String key,String value) {
        return stringRedisTemplate.opsForSet().add(key, value);
    }

    public Boolean isMember(String key,String value) {
        return stringRedisTemplate.opsForSet().isMember(key, value);
    }

    public Set<String> members(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }

    public Long remove(String key,String value) {
        return stringRedisTemplate.opsForSet().remove(key, value);
    }

}
